﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SimpleSchedule.Data;
using SimpleSchedule.Models;
using SimpleSchedule.Models.Configs;

namespace SimpleSchedule.Controllers
{
    [Authorize]
    public class ScheduleController : Controller
    {
        private readonly SSContext _db;
        private readonly Config _config;

        public ScheduleController(SSContext db, ConfigProvider configProvider)
        {
            _db = db;
            _config = configProvider.Config;
        }
        public IActionResult Index(string? search)
        {
            int autoHide = _config.AutoHide;
            List<Schedule> list;
            if (search is not null)
                list = _db.Schedules.Where(x => x.Remark != null && x.Remark.Contains(search) || x.Name.Contains(search)).ToList();
            else
                list = _db.Schedules.ToList();
            //list.RemoveAll(x => x.Target.AddDays(autoHide) < DateTime.Now);
            list.Sort((x, y) => DateTime.Compare(y.Target , x.Target));
            ViewData["search"] = search;
            return View(list);
        }
        [HttpGet]
        public IActionResult Add()
        {
            int defaultDays = _config.DefaultDays;
            Schedule sch = new()
            {
                Created = DateTime.Now.Date,
                Target = DateTime.Now.Date.AddDays(defaultDays)
            };
            ViewData["config"] = _config;
            return View(sch);
        }
        [HttpPost]
        public IActionResult Add(Schedule sch, int days, string? brand, string? spec, string staff)
        {
            if(days > 0)
                sch.Target = sch.Created.AddDays(days);
            List<string?> remarkInfo = [brand, spec, staff];
            remarkInfo.RemoveAll(string.IsNullOrEmpty);
            sch.Remark = string.Join("--", remarkInfo);
            _db.Schedules.Add(sch);
            _db.SaveChanges();
            return RedirectToAction(nameof(Index));
        }
        [HttpGet]
        public IActionResult Edit(int id)
        {
            var sch = _db.Schedules.Find(id);
            if (sch == null)
                return NotFound();
            return View(sch);
        }
        [HttpPost]
        public IActionResult Edit(Schedule sch)
        {
            _db.Schedules.Update(sch);
            _db.SaveChanges();
            return RedirectToAction(nameof(Index));
        }
        public IActionResult Duplicate(int id)
        {
            var oldSch = _db.Schedules.Find(id) ?? throw new Exception("找不到");
            var newSch = new Schedule()
            {
                Name = oldSch.Name,
                Created = oldSch.Created,
                Target = oldSch.Target,
                Remark = oldSch.Remark,
                Tel = oldSch.Tel,
            };
            _db.Schedules.Add(newSch);
            _db.SaveChanges();
            return RedirectToAction(nameof(Index));
        }
        public IActionResult Delete(int id)
        {
            _db.Schedules.Where(x => x.Id == id).AsQueryable().ExecuteDelete();
            return RedirectToAction(nameof(Index));
        }
    }
}
